package com.yangye.otherdemo.executordemo;

import java.util.concurrent.*;

/**
 * @author yangye
 * @date 2020/1/10 0010
 */
public class ExecutorDemo {

	static ExecutorService executorService = Executors.newFixedThreadPool(3);

	static ConcurrentHashMap<String, Future> threadPool = new ConcurrentHashMap<>();

	public static void main(String[] args) throws InterruptedException {
		for (int i = 1; i < 2; i++) {
			String name = "线程" + i;
			MyThread t = new MyThread(name);

			Future<?> submittedThread = executorService.submit(t);
			threadPool.put(name, submittedThread);
		}
		Thread.sleep(1000);
		Future t1 = threadPool.get("线程1");
		t1.cancel(true);
		System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++" + t1.isDone());
		System.out.println("===================================================" + t1.isCancelled());
//		executorService.shutdown();
	}

	static class MyThread implements Callable {

		private String name;

		public MyThread(String name) {
			this.name = name;
		}

		@Override
		public Object call() throws Exception {
			while (true) {
				if (Thread.currentThread().isInterrupted()) {
					break;
				}
				System.out.println("执行中");
			}
			return null;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}
	}

}
